package com.dk.controller;

import com.dk.pojo.Publisher;
import com.dk.pojo.TCountry;
import com.dk.service.CountryService;
import com.dk.service.PublisherService;
import com.dk.vo.PublisherVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("publisherController")
public class PublisherController {

    @Autowired
    private PublisherService publisherService;

    @Autowired
    private CountryService countryService;

    @RequestMapping("toList")
    public String toList(){
        return "publisher/list";
    }

    @RequestMapping("selectPublisher")
    @ResponseBody
    public Map<String,Object> selectUser(Publisher publisher , @RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "3")Integer pageSize){
        PageInfo<PublisherVo> page = publisherService.selectUser(publisher,pageNum,pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("rows",page.getList());
        return map;
    }
    @RequestMapping("toAddPublisher")
    public String toAdd(){
        return "publisher/add";
    }

    @RequestMapping("addPublisher")
    @ResponseBody
    public Boolean addUser(Publisher publisher){
        publisherService.addUser(publisher);
        return true;
    }
    @RequestMapping("updateInfo/{pubId}")
    public String updateInfo(@PathVariable("pubId")Integer pubId, Model model){
        Publisher t = publisherService.updateInfo(pubId);
        model.addAttribute("t",t);
        //查询出国家
        List<TCountry> provinceId = countryService.selectPro(0);
        model.addAttribute("provinceId",provinceId);
        //根据TUserCard表中的国家id查询出省
        List<TCountry> cityId = countryService.selectPro(t.getProvince());
        model.addAttribute("cityId",cityId);
        //根据TUserCard表中的省id查询出市
        List<TCountry> countryId = countryService.selectPro(t.getCity());
        model.addAttribute("countryId",countryId);
        return "publisher/update";
    }

    @RequestMapping("updateAll")
    @ResponseBody
    public Boolean updateAll(Publisher publisher){
        publisherService.updateAll(publisher);
        return true;
    }
    @RequestMapping("deleteInfo/{pubId}")
    @ResponseBody
    public Boolean deleteInfo(@PathVariable("pubId")Integer pubId){
        publisherService.deleteInfo(pubId);
        return true;
    }
}
